package com.ojoin.trade.common.utils.paynow;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import java.io.*;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: Twar3
 * Date: 15-1-3
 * Time: 下午5:47
 * To change this template use File | Settings | File Templates.
 */
public class HttpsExample {


    public static String httpsExample(String httpsURL)throws Exception{
        StringBuilder str = new StringBuilder();
        URL myUrl = new URL(httpsURL);
        HttpsURLConnection con = (HttpsURLConnection)myUrl.openConnection();
        InputStream ins = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader in = new BufferedReader(isr);
        String inputLine = "";
        while ((inputLine = in.readLine()) != null){
            str.append(inputLine);
        }
        in.close();
        return str.toString();
    }

    public static String sendHttps(String url,String reportContent,String charset,SSLContext sslContext){
        URL r_url = null;
        HttpsURLConnection urlConn = null;
        try {
            HostnameVerifier hv = new HostnameVerifier() {
                public boolean verify(String urlHostName, SSLSession session) {
                    return true;
                }
            };
            r_url = new URL(url);
            urlConn = (HttpsURLConnection) r_url.openConnection();
            urlConn.setSSLSocketFactory(sslContext.getSocketFactory());
            urlConn.setHostnameVerifier(hv);
            urlConn.setDoOutput(true);
            urlConn.setDoInput(true);
            urlConn.setRequestProperty("Charset", charset);
            urlConn.setRequestMethod("POST");
            OutputStream out = urlConn.getOutputStream();
            out.write(reportContent.getBytes(charset));
            out.flush();

            int status = urlConn.getResponseCode();
            //System.out.println("请求返回httpStatus为:"+status);
        } catch (Exception e) {
            //logger.error("发送Https请求异常" + e);
        }

        StringBuffer buffer = new StringBuffer();
        InputStream input = null;
        try {
            if(urlConn != null){
                input = urlConn.getInputStream();
                byte[] bt = new byte[1024];
                int length = -1;
                while ((length = input.read(bt)) != -1) {
                    buffer.append(new String(bt, 0, length, charset));
                }
            }
        } catch (Exception e) {
            //logger.error("接收Https响应数据异常" + e);
        }finally{
            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    //logger.error("Https数据接收完毕后，关闭流异常" + e);
                }
            }
        }

        String data = buffer.toString();
        //logger.info("Https 响应内容:"+data);

        return data;
    }

}
